package com.wj.blog.framework.object;

import com.wj.blog.framework.enums.ResponseCode;
import lombok.*;

import java.lang.ref.Reference;

/**
 * 统一结果返回类
 *
 * @ClassName ResponseVo
 * @Auther: wangjin
 * @Date: 2022/01/08/19:01
 * @Description: TODO
 * @Version 1.0
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@EqualsAndHashCode(callSuper = false)
public class ResponseVo<T> {
    /**
     * 状态码
     */
    private Integer code;

    /**
     * 提示信息
     */
    private String msg;

    /**
     * 数据
     */
    private T data;

    public ResponseVo(ResponseCode responseCode) {
        this.code = responseCode.getCode();
        this.msg = responseCode.getMessage();
    }

    public ResponseVo(ResponseCode responseCode, T data) {
        this.code = responseCode.getCode();
        this.msg = responseCode.getMessage();
        this.data = data;
    }

    /**
     * 统一结果返回
     *
     * @param responseCode 状态码
     * @param data         数据
     * @param <T>          数据类型
     * @return ResponseVo实例
     */
    public static <T> ResponseVo<T> result(ResponseCode responseCode, T data) {
        return ResponseVo.<T>builder()
                .code(responseCode.getCode())
                .msg(responseCode.getMessage())
                .data(data)
                .build();
    }

    /**
     * 统一结果返回
     *
     * @param responseCode 状态码
     * @return ResponseVo实例
     */
    public static ResponseVo result(ResponseCode responseCode) {
        return ResponseVo.builder()
                .code(responseCode.getCode())
                .msg(responseCode.getMessage())
                .build();
    }

    /**
     * 统一成功返回
     *
     * @param data 数据
     * @param <T>  数据类型
     * @return ResponseVo实例
     */
    public static <T> ResponseVo<T> success(T data) {
        ResponseCode success = ResponseCode.SUCCESS;
        return ResponseVo.<T>builder()
                .code(success.getCode())
                .msg(success.getMessage())
                .data(data)
                .build();
    }

    /**
     * 统一成功返回
     *
     * @return ResponseVo实例
     */
    public static ResponseVo success() {
        ResponseCode success = ResponseCode.SUCCESS;
        return ResponseVo.builder()
                .code(success.getCode())
                .msg(success.getMessage())
                .build();
    }

    /**
     * 统一错误返回
     *
     * @param data 数据
     * @param <T>  数据类型
     * @return ResponseVo实例
     */
    public static <T> ResponseVo<T> error(T data) {
        ResponseCode error = ResponseCode.SYS_ERROR;
        return ResponseVo.<T>builder()
                .code(error.getCode())
                .msg(error.getMessage())
                .data(data)
                .build();
    }

    /**
     * 统一错误返回
     *
     * @return ResponseVo实例
     */
    public static ResponseVo error() {
        ResponseCode error = ResponseCode.SYS_ERROR;
        return ResponseVo.builder()
                .code(error.getCode())
                .msg(error.getMessage())
                .build();
    }
}
